package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.heatup.apt.api.service.UserStoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heatup.apt.api.filter.ModelResApi;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;

@Controller
@RequestMapping("/user_store")
public class UserStoreServiceController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(UserStoreServiceController.class);
	@Autowired
	private UserStoreService userStoreService;

	@ResponseBody
	@RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET})
	private String getStoreByUserId(@ModelResApi ApiResultModel<Object> apiResultModel, HttpServletRequest request) {
//		ApiResultModel<Map<String, String>> apiResultModel = new ApiResultModel<Map<String, String>>();

		apiResultModel.setLogined(this.isLogined());
		String callback = RequestTools.RequestString(request, "callback", "");
		try {
			Map<String, Object> userStore = userStoreService.getUserStore(this.getUserId());
			if (userStore == null) {
				userStore = new HashMap<String, Object>();
			}
			apiResultModel.setData(userStore);
		} catch (Exception e) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("获取用户常去门店异常");
			logger.error("获取用户常去门店异常：", e);
		}

		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET})
	private String updateStoreByUserId(@ModelResApi ApiResultModel<Object> apiResultModel, HttpServletRequest request) {
//		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		apiResultModel.setLogined(this.isLogined());
		String callback = RequestTools.RequestString(request, "callback", "");
		Integer storeId = RequestTools.RequestInt(request, "favStoreId", 0);
		try {
			int result = userStoreService.update(storeId, this.getUserId());
			apiResultModel.setData(result);
		} catch (Exception e) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("更新用户常去门店信息出错");
			logger.error("更新用户常去门店信息出错", e);
		}

		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
}
